// pages/searchResult/searchResult.js
var app=getApp()
Page({
  data:{
    menuList:[
      {
        text: '价格排序',
        src: '../../images/searchIcon1-active.png',
        className: 'active'
      },
      {
        text: '销量优先',
        src: '',
        className: ''
      },
      {
        text: '筛选',
        src: '../../images/searchIcon2.png',
        className: ''
      }
    ],
    keywords: '',
    isFilter: false,
    latest:1
  },
  //实时变化input值
  keywordInput: function (e) {
    this.keywords = e.detail.value;
  },
  //点击搜索按钮
  toSearchResultWithKey: function (e) {
    wx.navigateTo({
      url: '../search-result/search-result?keywords=' + this.keywords
    });
  },
  change: function (e) {
    var teb = e.currentTarget.dataset.index;
    this.setData({
      latest: teb
    })
  },
  //风格
  selStyle: function (e) {
    var id = e.currentTarget.dataset.index
    var val = e.currentTarget.dataset.value
    this.setData({
      tstyle: val,
    })
  },
  //年龄段
  selAge: function (e) {
    var id = e.currentTarget.dataset.index
    var val = e.currentTarget.dataset.value
    this.setData({
      age: val,
    })
  },
  search:function(words,page,sort,that){
    //搜索结果
    var url = app.data.url
    wx.request({
      url: '' + url + '/shop/goods/search2',
      data: { word: words, page: 1,sort:sort },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
       // console.log(res)
        if (res.data.Rows.length > 0) {
          var glist = res.data.Rows
          that.setData({
            glist: glist
          })
        } else {
          wx.showToast({
            title: '亲，没有符合条件的商品哦',
          })
        }
      },
      error: function (res) {
        console.log(res)
      }
    })
  },
  onLoad:function(options){
    var that=this
    var url=app.data.url
    var words=options.keywords
    that.setData({
      words:words,
      url:url
    })
    that.search(words,1,'',that)
    //风格，年龄段
    var url = app.data.url
    wx.request({
      url: '' + url + '/Shop/Goods/GetStyleAndAge',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        var style=res.data
        that.setData({
          style:style
        })
      },
      error: function (res) {
        console.log(res)
      }
    })

  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },

  /**
   * 点击切换筛选条件
   */
  changeActive: function(e){
    console.log(e);
    var words = this.data.words
    let index = e.currentTarget.dataset.index;
    const length = this.data.menuList.length;

    if(index == 2){
      this.isFilter = true;
    }else{
      this.isFilter = false;
    }
    for(let i = 0; i< length;i++){
      if(index == i){
        this.data.menuList[i].className = 'active';
        if(index == 0){
          this.data.menuList[0].src = '../../images/searchIcon1-active.png';
          this.search(words, 1, '', this)
        }else{
          this.data.menuList[0].src = '../../images/searchIcon1.png';
        }
        if (index == 1) {
          this.search(words, 1, 1, this)
        } else {
          
        }
        if(index == 2){
          this.data.menuList[2].src = '../../images/searchIcon2-active.png';
        }else{
          
          this.data.menuList[2].src = '../../images/searchIcon2.png';
        }
      }else{

        this.data.menuList[i].className = '';
      }
    }

    this.setData({
      menuList: this.data.menuList,
      isFilter: this.isFilter
    })
  },
  //clear input
  clearInput: function(e){
    this.keywords = '';
    this.setData({
      keywords: this.keywords
    });
  },
  /**
   * 隐藏筛选
   */
  hideFilter: function(e){
    var url=app.data.url;
    this.isFilter = false;
    this.setData({
      isFilter: this.isFilter
    })
    var formData=e.detail.value
    wx.request({
      url: '' + url + '/shop/goods/search2',
      data: formData,
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        // console.log(res)
        if (res.data.Rows.length > 0) {
          var glist = res.data.Rows
          that.setData({
            glist: glist
          })
        } else {
          wx.showToast({
            title: '亲，没有符合条件的商品哦',
          })
        }
      },
      error: function (res) {
        console.log(res)
      }
    })
  }
})